<template>
  <div class="app-container">

    <el-form :model="queryForm" ref="queryForm" :inline="true" v-show="showSearch" label-width="80">

      <el-form-item label="字段名称">
        <el-input v-model="queryForm.flowName" placeholder="请输入字段名称" clearable />
      </el-form-item>

      <el-form-item label="数据库名称">
        <el-input v-model="queryForm.databaseName" placeholder="请输入数据库名称" clearable />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="handleQuery" icon="el-icon-search" size="mini">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['contract:ice-flow:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['contract:ice-flow:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['contract:ice-flow:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>

    </el-row>

    <el-table :data="dataList" @selection-change="handleSelectionChange" fit border width="100%">
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column label="ID" align="center" prop="id" fixed width="100" show-overflow-tooltip />
      <el-table-column label="字段名称" prop="flowName" align="center" show-overflow-tooltip />
      <el-table-column label="数据库名称" prop="databaseName" align="center" show-overflow-tooltip />
      <el-table-column label="配置类型" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flow_conf_type" :value="scope.row.confType" />
        </template>
      </el-table-column>
      <el-table-column label="配置名称" prop="confName" align="center" show-overflow-tooltip />
      <el-table-column label="备注" prop="remark" align="center" show-overflow-tooltip />
      <el-table-column label="创建人" prop="createBy" align="center" show-overflow-tooltip />
      <el-table-column label="创建时间" prop="createTime" align="center" show-overflow-tooltip />
      <el-table-column label="更新人" prop="updateBy" align="center" show-overflow-tooltip />
      <el-table-column label="更新时间" prop="updateTime" align="center" show-overflow-tooltip />
    </el-table>

     <pagination
       v-show="total>0"
       :total="total"
       :page.sync="queryForm.pageNum"
       :limit.sync="queryForm.pageSize"
       @pagination="getList"
     />

     <el-dialog :title="title" :visible.sync="open" width="350px" append-to-body :close-on-click-modal="false">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="字段名称" prop="flowName">
            <el-input v-model="form.flowName" clearable style="width: 200px"/>
          </el-form-item>
          <el-form-item label="数据库名称" prop="databaseName">
            <el-input v-model="form.databaseName" clearable style="width: 200px"/>
          </el-form-item>
          <el-form-item label="配置类型" prop="confType">
            <el-select v-model="form.confType" placeholder="请选择配置类型" style="width: 200px">
              <el-option
                v-for="dict in dict.type.flow_conf_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" clearable style="width: 200px"/>
          </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>

     </el-dialog>

  </div>
</template>

<script>
import {delFlow, getFlowById, listFlow, saveFlow, updateFlow} from "@/api/base/flow";
import {addSerial, delSerial, getSerial, updateSerial} from "@/api/base/serial";

export default {
  name: "IceFlow",
  dicts: ['flow_conf_type'],
  data() {
    return {
      showSearch:true,
      dataList: [],
      total: 0,
      queryForm: {
        pageNum: 1,
        pageSize: 10,
      },
      form: {
        id: null,
        flowName: '',
        confName: '',
        databaseName: '',
        confType: null,
        remark: '',
      },
      single: true,
      multiple: true,

      title: '',
      open: false,

      rules: {
        flowName: [
          {required: true, message: "字段名称不能为空", trigger: "blur"}
        ],
        databaseName: [
          {required: true, message: "数据库名称不能为空", trigger: "blur"}
        ],
        confType: [
          {required: true, message: "配置类型不能为空", trigger: "blur"}
        ],
      }

    }
  },
  created() {
    this.handleQuery()
  },
  methods: {
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getList();
    },
    getList() {
      listFlow(this.queryForm).then(res => {
        this.total = res.total
        this.dataList = res.rows
      })
    },
    resetQuery() {
      this.queryForm= {
        pageNum: 1,
        pageSize: 10,
      };
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFlow(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            saveFlow(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        flowName: '',
        confName: '',
        databaseName: '',
        confType: null,
        remark: '',
      };
      this.resetForm("form");
    },
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增节点判断";
    },
    handleUpdate(){
      this.reset();
      const id =  this.ids
      getFlowById(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改节点判断";
      });
    },
    handleDelete(){
      const ids = this.ids;
      this.$modal.confirm('是否确认删除节点判断编号为"' + ids + '"的数据项？').then(function() {
        return delFlow(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  },
}
</script>

<style scoped>

</style>
